<?php

include 'UpFile.class.php';
include ("../conn.php");

$id = "";
if(isset($_GET['id'])){
	$id = $_GET['id'];
}
$parentid = "";
if(isset($_GET['parentid'])){
	$parentid = $_GET['parentid'];
}
$parentname = "";
if(isset($_GET['parentname'])){
	$parentname = $_GET['parentname'];
}
$act = "";
if(isset($_GET['act'])){
	$act = $_GET['act'];
}
$opertype = "";
if(isset($_GET['opertype'])){
	$opertype = $_GET['opertype'];
}
if($act=="")$act = "create";//update
if($opertype=="") $opertype = "main";//file

//保存或修改产品信息
if(isset($_POST['submit']) && $opertype=="main"){
	//要传入 主键 和外键
	$id = $_POST['ID'];
	$tablename = $databasepre."product";
	$insertStr1 = "";
	$insertStr2 = "";
	$updateStr = "";

	$selectSQL = "select upper(column_name) as column_name ,upper(data_type) as column_type,character_maximum_length as column_length  from  information_schema.columns where table_name='".$tablename."'";
	//echo $selectSQL;
	$query = mysql_query($selectSQL);
	$i = 0;
	while($row=mysql_fetch_array($query))
	{
		$sep = ",";
		$column_type = $row['column_type'];
		$column_name = $row['column_name'];
		$column_value = "";
		if(isset($_POST["$column_name"])){
			$column_value = mysql_real_escape_string($_POST["$column_name"]);
		}
		if(isset($_POST["$column_name"]) ){//isset($_POST["$column_name"])  && $column_value!=""
			if($i==0){
				$i +=1;
				$sep = "";
			}
			$insertStr1 .= $sep.$column_name;
			if($column_type=="VARCHAR" || $column_type=="TEXT" || $column_type=="DATE"){
				$insertStr2 .= $sep."'".$column_value."'";
				$updateStr .= $sep.$column_name."='".$column_value."'";
			}else if($column_type == "INT"){
				$insertStr2 .= $sep."'".$column_value."'";
				$updateStr .= $sep.$column_name."='".$column_value."'";
			}
		}
	}
	if($id==0 || $id==""){
		$insertSQL = "insert into ".$tablename."(".$insertStr1.",CREATEDATE) values (".$insertStr2.",now())";
		//echo $insertSQL;
		mysql_query($insertSQL);
		$id = mysql_insert_id();
	}else{
		$updateSQL = "update ".$tablename." set ".$updateStr.",UPDATEDATE=now() where id=".$id;// a,d,f,g    'sd','sds','sdsddd'
		//echo $updateSQL;
		mysql_query($updateSQL);
	}
	echo "操作成功"."<br/><a href='./admin_product_add.php?parentid=".$parentid."&parentname=".urlencode($parentname)."&id=".$id."'>返回</a>";
	echo "<br/><a href='./admin_product_list.php?parentid=".$parentid."&parentname=".urlencode($parentname)."&id=".$id."'>返回列表</a>";
	exit;
}

//图片新增
if(isset($_POST['submit']) && $opertype=="file" && $act=="create"){
	
	if(@empty($_FILES["FORIGINALFILE"]["name"])) die("没有上传文件信息，请确认！<br/><a href='./admin_product_add.php?opertype=file&parentid=".$parentid."&parentname=".urlencode($parentname)."&id=".$id."'>返回</a>");

	$title = trim(mysql_real_escape_string($_POST['FTITLE']));
	$seq = intval($_POST['SEQ']);

	$upFile= new UpFile(); //实例化类文件
	$photo = $upFile->upLoad("FORIGINALFILE"); //上传的文件域

	$upFile->toFile = true;
	$upFile->saveBigImg($photo);
	$upFile->saveSmallImg($photo,250,250);

	//入库
	$sql =  "insert into ".$databasepre."file(FTITLE,FORIGINALFILE,SEQ,PARENTID) values ('".$title."','".$photo."',".$seq.",".$id.") ";
	//echo $sql;
	mysql_query($sql);
	echo "<script>window.location.href='./admin_product_add.php?opertype=file&parentid=".$parentid."&parentname=".urlencode($parentname)."&id=".$id."';</script>";
}

//删除操作
if(!empty($_GET['fid']) && $opertype=="file" && $act=="del")
{
	$selsql = "select FORIGINALFILE from ".$databasepre."file where ID=".$_GET['fid'];
	$query = mysql_query($selsql);
	$row=mysql_fetch_array($query);
	try{
		@unlink("../upload/original/".$row['FORIGINALFILE']);
		@unlink("../upload/big/".$row['FORIGINALFILE']);
		@unlink("../upload/small/".$row['FORIGINALFILE']);
	}catch (Exception $e) {
    	echo "遇到异常，但程序会继续执行。异常信息：". $e->getMessage();
    	exit;
	}


	$delsql="delete from ".$databasepre."file where ID=".$_GET['fid'];
	//echo $delsql;
	mysql_query($delsql);
	
	echo "操作成功！<br/><a href='./admin_product_add.php?opertype=file&parentid=".$parentid."&parentname=".urlencode($parentname)."&id=".$id."'>返回</a>";
	exit;
	
	//echo "<script>window.location.href='./admin_product_add.php?act=create&opertype=file&parentid=".$parentid."&parentname=".urlencode($parentname)."&id=".$id."'</script>";
}
?>
<html><head>
<meta http-equiv=Content-Type content=text/html;charset=utf-8>
<link href="images/skin.css" rel="stylesheet" type="text/css" />


<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color: #EEF2FB;
}
.td1{
	width:25%;
	text-align:right;
}
.td2{
	width:25%;
}
.td3{
	width:2%;

}
.td4{
	width:20%;
	text-align:right;
}
.td5{
	width:25%;
}
-->
</style>
<script language="JavaScript" type="text/javascript" src="../plugin/jquery/jquery-1.4.2.min.js"></script>
<script language="JavaScript" type="text/javascript" src="../plugin/xheditor/xheditor-zh-cn.min.js?v=1.1.1"></script>

<script language="JavaScript" type="text/javascript">
jQuery(document).ready(function(){
	onchangeShowcase()
});

function onchangeShowcase(){
	var isshow = jQuery("#ID_ISSHOWCASE").val();
	if(isshow!=1)isshow = jQuery("#ID_ISSHOWCASE2").val();
	if(isshow=="1"){
		jQuery("#TR_SHOWCASEINFO").show();
	}else{
		jQuery("#TR_SHOWCASEINFO").hide();
	}
}
function manageFile(){
	var id = jQuery("#ID_ID").val();
	if(id=="" || id==0){
		alert("请先保存基本信息，在管理图片！");
		return false;
	}

}
</script>

</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="17" valign="top" background="images/mail_leftbg.gif"><img src="images/left-top-right.gif" width="17" height="29" /></td>
    <td valign="top" background="images/content-bg.gif">
		<table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="left_topbg" id="table2">
		  <tr><td height="31"><div class="titlebt">产品管理</div></td></tr>
		</table>
	</td>
    <td width="16" valign="top" background="images/mail_rightbg.gif"><img src="images/nav-right-bg.gif" width="16" height="29" /></td>
  </tr>
  <tr>
    <td valign="middle" background="images/mail_leftbg.gif">&nbsp;</td>
    <td valign="top" bgcolor="#F7F8F9">
	<!-- 网页内容部分开始 -->


	<table width="100%" height="138" border="0" cellpadding="0" cellspacing="0">
      <tr><td height="13" valign="top">&nbsp;</td></tr>
	  <tr><td class="left_txt">当前位置：产品分类管理 - > <a href="admin_product_list.php?parentid=<?php echo $parentid ?>&parentname=<?php echo urlencode($parentname) ?>"><?php echo $parentname ?></a> </td></tr>
	  <!-- 一根线 -->
	  <tr><td height="20"><table width="100%" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC"><tr><td></td></tr></table></td></tr>
	  <!-- 说明信息 -->
	  <tr><td><table width="100%" height="55" border="0" cellpadding="0" cellspacing="0"><tr>
				<td width="10%" height="55" valign="middle"><img src="images/title.gif" width="54" height="55"></td>
				<td width="90%" valign="top"><span class="left_txt2">在这里，您可以根据您的网站要求，修改设置网站的关于我们的内容！</span>
						[<a href="admin_product_add.php?act=create&parentid=<?php echo $parentid ?>&parentname=<?php echo urlencode($parentname) ?>"><b><u>快捷新增</u></b></a>]
				</td>
				</tr></table>
		</td>
	  </tr>
	  <tr><td>&nbsp;</td></tr>
	  <tr><td><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="nowtable">
				  <tr>
				  		<td class="left_bt2" width=15% bgcolor="<?php echo $opertype=="main"?"#9999FF":"#C0C0C0" ?>" align=center>&nbsp;<a href="?opertype=main&parentid=<?php echo $parentid ?>&parentname=<?php echo urlencode($parentname) ?>&id=<?php echo $id ?>">产品基本管理</a></td>
				  		<td class="left_bt2" width=15% bgcolor="<?php echo $opertype=="file"?"#9999FF":"#C0C0C0" ?>" align=center><a href="?opertype=file&parentid=<?php echo $parentid ?>&parentname=<?php echo urlencode($parentname) ?>&id=<?php echo $id ?>" onClick="return manageFile();">产品图片管理</a></td>
				  		<td class="left_bt2" width=70%>&nbsp;</td>
				  </tr>
				</table>
		</td>
      </tr>
	  <tr><td><table width="98%" border="0" cellspacing="10" cellpadding="0">
				<form method=POST action="" enctype="multipart/form-data">
				<?php
					if($opertype=="main") {
					$row;
					$status=0;
					if($id!=""){
						$status=1;
						$selectSQL="select * from ".$databasepre."product WHERE ID=".$id;
						$query=mysql_query($selectSQL);
						$row=mysql_fetch_array($query);
					}
				?>

					<input type="hidden" name="ID" value="<?php if($status==1){echo $row['ID']==""?0:$row['ID'];} ?>" id="ID_ID"/>
					<input type="hidden" name="PARENTID" value="<?php echo $parentid ?>"/>
					<tr>
						<td class="td1">名称[英文]：</td>
						<td class="td2" align=left ><input type="text" name="PNAME" size="40" value="<?php  if($status==1){echo $row['PNAME'];}  ?>"/></td>
						<td class="td3" align=left></td>
						<td class="td4" >名称[中文]：</td>
						<td class="td5" align=left ><input type="text" name="PNAME2" size="40" value="<?php  if($status==1){echo $row['PNAME2'];} ?>"/></td>

					</tr>
					<tr>
						<td height="30" colspan="5" bgcolor="#99CCFF" style="font-size=12px;font-weight:bold;"><b>支付方式和购物条款[Product Details]</b></td>
					</tr>
					<tr>
						<td class="td1">原产地[Place of Origin]：</td>
						<td class="td2"><input type="text" name="PPLACE" size="40" value="<?php  if($status==1){echo $row['PPLACE'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">原产地：</td>
						<td class="td5"><input type="text" name="PPLACE2" size="40" value="<?php  if($status==1){echo $row['PPLACE2'];} ?>"/></td>
					</tr>
					<tr>
						<td class="td1">品牌[Brand Name]：</td>
						<td class="td2"><input type="text" name="PBRAND" size="40" value="<?php  if($status==1){echo $row['PBRAND'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">品牌：</td>
						<td class="td5"><input type="text" name="PBRAND2" size="40" value="<?php  if($status==1){echo $row['PBRAND2'];} ?>"/></td>
					</tr>
					<tr>
						<td class="td1">型号[Model Number]：</td>
						<td class="td2"><input type="text" name="PMODEL" size="40" value="<?php  if($status==1){echo $row['PMODEL'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">型号：</td>
						<td class="td5"><input type="text" name="PMODEL2" size="40" value="<?php  if($status==1){echo $row['PMODEL2'];} ?>"/></td>
					</tr>

					<tr>
						<td class="td1">类型[Type]：</td>
						<td class="td2"><input type="text" name="PTYPE" size="40" value="<?php  if($status==1){echo $row['PTYPE'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">类型：</td>
						<td class="td5"><input type="text" name="PTYPE2" size="40" value="<?php  if($status==1){echo $row['PTYPE2'];} ?>"/></td>
					</tr>
					<tr>
						<td class="td1">材料[Material]：</td>
						<td class="td2"><input type="text" name="PMATERIAL" size="40" value="<?php  if($status==1){echo $row['PMATERIAL'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">材料：</td>
						<td class="td5"><input type="text" name="PMATERIAL2" size="40" value="<?php  if($status==1){echo $row['PMATERIAL2'];} ?>"/></td>
					</tr>
					<tr>
						<td class="td1">尺寸[Size]：</td>
						<td class="td2"><input type="text" name="PSTYLE" size="40" value="<?php  if($status==1){echo $row['PSTYLE'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">尺寸：</td>
						<td class="td5"><input type="text" name="PSTYLE2" size="40" value="<?php  if($status==1){echo $row['PSTYLE2'];} ?>"/></td>
					</tr>
					<tr>
						<td class="td1">颜色[Color]：</td>
						<td class="td2"><input type="text" name="PCOLOR" size="40" value="<?php  if($status==1){echo $row['PCOLOR'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">颜色：</td>
						<td class="td5"><input type="text" name="PCOLOR2" size="40" value="<?php  if($status==1){echo $row['PCOLOR2'];} ?>"/></td>
					</tr>
					<tr>
						<td height="30" colspan="5" bgcolor="#99CCFF" style="font-size=12px;font-weight:bold;"><b>支付方式和购物条款[Payment & Shipping Terms]</b></td>
					</tr>
					<tr>
						<td class="td1">最小订购量[Minimum Order Quantity]：</td>
						<td class="td2"><input type="text" name="PMINNUM" size="40" value="<?php  if($status==1){echo $row['PMINNUM'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">最小订购量：</td>
						<td class="td5"><input type="text" name="PMINNUM2" size="40" value="<?php  if($status==1){echo $row['PMINNUM2'];} ?>"/></td>
					</tr>
					<tr>
						<td class="td1">口岸[Port]：</td>
						<td class="td2"><input type="text" name="PPORT" size="40" value="<?php  if($status==1){echo $row['PPORT'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">口岸：</td>
						<td class="td5"><input type="text" name="PPORT2" size="40" value="<?php  if($status==1){echo $row['PPORT2'];} ?>"/></td>
					</tr>
					<tr>
						<td class="td1">打包信息[Packaging Details]：</td>
						<td class="td2"><input type="text" name="PPACKAGEING" size="40" value="<?php  if($status==1){echo $row['PPACKAGEING'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">打包信息：</td>
						<td class="td5"><input type="text" name="PPACKAGEING2" size="40" value="<?php  if($status==1){echo $row['PPACKAGEING2'];} ?>"/></td>
					</tr>

					<tr>
						<td class="td1">支付方式[Payment Terms]：</td>
						<td class="td2"><input type="text" name="PPAYMENT" size="40" value="<?php  if($status==1){echo $row['PPAYMENT'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">支付方式：</td>
						<td class="td5"><input type="text" name="PPAYMENT2" size="40" value="<?php  if($status==1){echo $row['PPAYMENT2'];} ?>"/></td>
					</tr>
					<tr>
						<td class="td1">发货时间[Delivery Time]：</td>
						<td class="td2"><input type="text" name="DELIVERYTIME" size="40" value="<?php  if($status==1){echo $row['DELIVERYTIME'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">发货时间：</td>
						<td class="td5"><input type="text" name="DELIVERYTIME2" size="40" value="<?php  if($status==1){echo $row['DELIVERYTIME2'];} ?>"/></td>
					</tr>
					<tr>
						<td class="td1">供应能力[Supply Ability]：</td>
						<td class="td2"><input type="text" name="PSUPPLYABILITY" size="40" value="<?php  if($status==1){echo $row['PSUPPLYABILITY'];} ?>"/></td>
						<td class="td3"></td>
						<td class="td4">供应能力：</td>
						<td class="td5"><input type="text" name="PSUPPLYABILITY2" size="40" value="<?php  if($status==1){echo $row['PSUPPLYABILITY2'];} ?>"/></td>
					</tr>
					<tr>
						<td height="30" colspan="5" bgcolor="#99CCFF" style="font-size=12px;font-weight:bold;"><b>详细描述[Detailed Product Description]</b></td>
					</tr>
					<tr>
						<td class="td1">加入展柜[Product Showcase]：</td>
						<td class="td2">
							<select name="ISSHOWCASE" id="ID_ISSHOWCASE" onChange="onchangeShowcase();">
								<option value="0" <?php if($status==1){echo $row['ISSHOWCASE']=="0"?"selected":"";} ?>>否</option>
								<option value="1" <?php if($status==1){echo $row['ISSHOWCASE']=="1"?"selected":"";} ?>>是</option>
							</select>
					  	</td>
						<td class="td3"></td>
						<td class="td4">加入展柜：</td>
						<td class="td5">
							<select name="ISSHOWCASE2" id="ID_ISSHOWCASE2" onChange="onchangeShowcase();">
								<option value="0" <?php if($status==1){echo $row['ISSHOWCASE2']=="0"?"selected":"";} ?>>否</option>
								<option value="1" <?php if($status==1){echo $row['ISSHOWCASE2']=="1"?"selected":"";} ?>>是</option>
							</select>
					  	</td>
					</tr>
					<tr id="TR_SHOWCASEINFO" style="display:none;">
						<td class="td1">展柜说明：</td>
						<td class="td2"><textarea name="SHOWCASEINFO" rows="4" cols="33"><?php  if($status==1){echo $row['SHOWCASEINFO'];} ?></textarea></td>
						<td class="td3"></td>
						<td class="td4">展柜说明：</td>
						<td class="td5"><textarea name="SHOWCASEINFO2" rows="4" cols="33"><?php  if($status==1){echo $row['SHOWCASEINFO2'];} ?></textarea></td>
					</tr>
					<tr>
						<td colspan=2><textarea name="PCONTENT" class="xheditor-mfull"  style="width: 100%" rows="12"><?php  if($status==1){echo $row['PCONTENT'];} ?></textarea></td>
						<td class="td3"></td>
						<td colspan=2><textarea name="PCONTENT2"   class="xheditor-mfull"  style="width: 100%" rows="12"><?php  if($status==1){echo $row['PCONTENT2'];} ?></textarea></td>
					</tr>

					<tr>
						<td>显示顺序：<input type="text" name="SEQ" size="10" value="<?php  if($status==1){echo $row['SEQ'];} ?>"/></td>
						<td align=left colspan=3><input type="submit" name="submit" value=" 确 定 " /></td>
						<td></td>
					</tr>
				<?php }else if($opertype=="file") {

					if(empty($id)){
						echo '请先保存产品基本信息，再管理图片！';

						echo '<a href="?opertype=main&parentid='.$parentid.'&parentname='.urlencode($parentname).'&id='.$id.'">返回</a>';

						exit;
					}

					?>

					<tr bgcolor="#f2f2f2">
						<th width="100" class="left_txt" align=left></th>
						<th width="200" height="30" class="left_txt2" align=left>名称</th>
						<th width="200" class="left_txt2" align=left>路径</th>
						<th width="100" class="left_txt2" align=left>排序[升序]</th>
						<th width="100" class="left_txt2" align=left>操作</th>
					</tr>
					<?php 
					//if($act=="create") { 
					?>
					<tr>
					<td height="30"></td>
					<td align=left><input type="text" name="FTITLE" value="" size="20" maxlength="40"/></td>
					<td align=left><input type="file" name="FORIGINALFILE" value="" size="20" maxlength="40"/></td>
					<td align=left><input type="text" name="SEQ" value="" size="5" maxlength="40"/></td>
					<td align=left><input type="submit" name="submit" value=" 新 增 " /></td>
					</tr>
					<?php
					//}
					$tdstyle="";
					$tdi = 0;
					$sql="select * from ".$databasepre."file where PARENTID=".$id." order by SEQ asc,ID asc";
					//echo $sql;
					$query=mysql_query($sql);
					while($row=mysql_fetch_array($query))
					{
						$tdi = $tdi + 1;
						if($tdi%2 == 1) { $tdstyle = "f2f2f2"; }else{ $tdstyle = ""; }
						echo '<tr bgcolor='.$tdstyle.'>';
						echo '<td height="30"></td>';
						echo '<td align=left>'.$row['FTITLE'].'</td>';
						echo '<td align=left><img src="../upload/small/'.$row['FORIGINALFILE'].'" border="0"></td>';
						echo '<td align=left>'.$row['SEQ'].'</td>';
						echo '<td align=left> <a href=?opertype=file&act=del&parentid='.$parentid.'&parentname='.urlencode($parentname).'&id='.$id.'&fid='.$row['ID'].'> 删除 </a></td>';
						echo '</tr>';
					}

				}
					?>


				</form>
			  </table>
			</td>
		</tr>

	 </table>


	<!-- 网页内容部分结束 -->
	</td>
    <td background="images/mail_rightbg.gif">&nbsp;</td>
  </tr>
  <tr>
    <td valign="bottom" background="images/mail_leftbg.gif"><img src="images/buttom_left2.gif" width="17" height="17" /></td>
    <td background="images/buttom_bgs.gif"><img src="images/buttom_bgs.gif" width="17" height="17"></td>
    <td valign="bottom" background="images/mail_rightbg.gif"><img src="images/buttom_right2.gif" width="16" height="17" /></td>
  </tr>
</table>
</body>
</html>
